package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;
import jdk.jfr.MetadataDefinition;

import java.io.File;
import java.util.List;

/**
 * 整合数据库操作
 */
public class DataBaseManager {
    //从 Spring 中拿到现成的对象
    private MetaMapper metaMapper;

    //对数据库进行初始化
    public void init(){
        //手动获取到 MetaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);

        if(!checkDBExists()){
            //先创建 data 目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            //数据库不存在，建表
            createTable();
            //插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成！");
        }else{
            System.out.println("[DataBaseManager] 数据库已存在！");
        }
    }

    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean result = file.delete();
        if(result){
            System.out.println("[DataBaseManager] 删除数据库文件成功！");
        }else{
            System.out.println("[DataBaseManager] 删除数据库文件失败！");
        }
        File dataDir = new File("./data");
        // delete 删除目录时要保证目录是空的
        result = dataDir.delete();
        if(result){
            System.out.println("[DataBaseManager] 删除数据库目录成功！");
        }else{
            System.out.println("[DataBaseManager] 删除数据库目录失败！");
        }
    }

    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 这个方法用来建表
     * 建库操作不需要手动执行
     * 首次执行这里的数据库操作的时候会自动创建出 meta.db 的文件 (MyBatis 完成的)
     */
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成！");
    }

    /**
     * 给数据库中的表添加默认数据
     * 添加一个默认交换机：匿名 的交换机，类型为 DIRECT
     */
    private void createDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManage] 创建初始数据完成！");
    }

    /**
     * 封装其他的数据库的操作
     */
    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }

    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }

    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }
}
